home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / c / gaplib_beta / wizards / templates / bitstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-14  |  1.6 KB  |  90 lines

  1.  
  2.  
  3. void    Init$N(struct $N *Polly)    $2/* Initialization function. */$
  4. {
  5. int    i;
  6.  
  7. #ifdef    ZINIT$I
  8. for(i=0;i!=SIZE$I;i++) {
  9.     ((char *)Polly->Data)[i]=0;    $2/* Zero init */$
  10. }
  11. #else
  12. for(i=0;i!=SIZE$I;i++) {
  13.     ((char *)Polly->Data)[i]=Rnd(256);    $2/* Random init */$
  14. }
  15. #endif
  16. }
  17.  
  18. void Mutate$N(struct $N *Polly)
  19. {
  20. int i;
  21. for(i=0;i!=(SIZE$I<<3);i++) {
  22.     if (Rnd (1024) == 512) {
  23.         Flip(Polly,(long)i);    $3/* Flip a random bit one in 1024 times. */$
  24. $4/*
  25.    NAME
  26.         Flip -- Flip a bit in a bitstring.
  27.  
  28.    SYNOPSIS
  29.         void Flip(void *,int);
  30.  
  31.         Flip(Ind,At);
  32.  
  33.    FUNCTION
  34.         Flips a bit in a bitstring. Bits are counted from lower addresses to
  35.         higher.
  36. */
  37. $    }
  38. }
  39. }
  40.  
  41. void Cross$N(struct $N *Polly,struct $N *Tweety)
  42. {
  43. #ifdef    MPCROSS$I
  44. int i;
  45.  
  46. for(i=0;i!=(SIZE$I<<3);i++) {
  47.     if(Rnd(128)==64) {
  48.         Crossover(Polly,Tweety,i,(long)SIZE$I);
  49.     }
  50. }
  51. #else
  52. Crossover(Polly,Tweety,Rnd((long)SIZE$I<<3),(long)SIZE$I);
  53. #endif
  54. $4/*
  55.    NAME
  56.         Crossover -- Perform crossover on two bitstrings.
  57.  
  58.    SYNOPSIS
  59.         void Crossover(void *,void *,int,int);
  60.  
  61.         Crossover(void *Ind1,void *Ind2,int At,int Size);
  62.  
  63.    FUNCTION
  64.         Performs one-point crossover of two bitstrings. The bitstrings must
  65.         have the same length.
  66. */
  67. $}
  68.  
  69. double Compare$N(struct $N *Polly,struct $N *Tweety,int i)
  70. {
  71. return((double)HammingDist(Polly,Tweety,i));
  72. $4/*
  73.    NAME
  74.         HammingDist -- Measure the Hamming distance between two bitstrings.
  75.  
  76.    SYNOPSIS
  77.         unsigned long int HammingDist(void *,void *,int);
  78.  
  79.         distance = HammingDist(Ind1,Ind2,Size);
  80.  
  81.    FUNCTION
  82.         Counts the number of differings bits in two bitstrings.
  83. */
  84. $}
  85.  
  86. void Kill$N(struct $N *Polly)
  87. {
  88. ;
  89. }
  90.